package com.gz.EuchreU.Helpers;

import org.anddev.andengine.util.MathUtils;

import com.badlogic.gdx.math.Vector2;

public class HelperMethods {

	public static Vector2 getVelocityFromPointToPoint(Vector2 point1, Vector2 point2, float totalVelocity)
	{		
		Vector2 localTouch = new Vector2(point2.x - point1.x, point2.y - point1.y);
		
		double theta = getAngleToPoint(localTouch);
				
		float hypotenuse = totalVelocity;
		float velocityX = (float) (hypotenuse * Math.cos(theta));
		float velocityY = (float) (hypotenuse * Math.sin(theta));
		
		if(localTouch.x < 0 && velocityX > 0) velocityX = -velocityX;
		else if(localTouch.x > 0 && velocityX < 0) velocityX = -velocityX;
		
		if(localTouch.y < 0 && velocityY > 0) velocityY = -velocityY;
		else if(localTouch.y > 0 && velocityY <0)velocityY = -velocityY; 
		
		return new Vector2(velocityX, velocityY);
		
	}

	public static double getDistanceFromPointToPoint(Vector2 point1, Vector2 point2){
		float adjacent = point2.x - point1.x;
		float opposite = point2.y - point1.y;
		
		adjacent *= adjacent;
		opposite *= opposite;
		
		return Math.sqrt(adjacent + opposite);
	}
	
	public static Vector2 getVelocityToAngle(double theta, float totalVelocity){
		float hypotenuse = totalVelocity;
		float velocityX = (float) (hypotenuse * Math.cos(theta));
		float velocityY = (float) (hypotenuse * Math.sin(theta));
		
//		if(localTouch.x < 0 && velocityX > 0) velocityX = -velocityX;
//		else if(localTouch.x > 0 && velocityX < 0) velocityX = -velocityX;
//		
//		if(localTouch.y < 0 && velocityY > 0) velocityY = -velocityY;
//		else if(localTouch.y > 0 && velocityY <0)velocityY = -velocityY; 
		
		return new Vector2(velocityX, velocityY);
	}
	
	public static float getAnglePointToPoint(Vector2 point1, Vector2 point2){
		return MathUtils.radToDeg((float) getAngleFromPointToPoint(point1, point2));
	}
	
	public static double getAngleFromPointToPoint(Vector2 point1, Vector2 point2)
	{
		Vector2 localTouch = new Vector2(point2.x - point1.x, point2.y - point1.y);
		return getRadToPoint(localTouch);
	}
	
	private static double getRadToPoint(Vector2 point)
	{
		return Math.atan2(point.x, point.y);
	}
	
	private static double getAngleToPoint(Vector2 point)
	{
		float oa = point.y / point.x;
		double theta = Math.atan(oa);
		
		return theta;
	}

	public static Vector2 GetVelocityFromAngle(final float angle, final float power) {
		float velX = 0;
		float velY = 0;
		float theta = angle;

		int quadrant = 0;

		while (theta >= 360)
			theta -= 360;
		while (theta < 0)
			theta += 360;

		if (theta >= 270) {
			quadrant = 2;
			theta = 360 - theta;
		} else if (theta >= 180) {
			quadrant = 3;
			theta = 270 - theta;
		} else if (theta >= 90) {
			quadrant = 4;
			theta = 180 - theta;
		} else {
			quadrant = 1;
			theta = 90 - theta;
		}

		float adjacent = (float) Math.cos(Math.toRadians(theta));
		float opposite = (float) Math.sin(Math.toRadians(theta));

		adjacent *= power;
		opposite *= power;

		// log("VelFromAngle: " + angle, "theta:" + theta + "a:" + adjacent +
		// " o:" + opposite + " Q:" + quadrant + " p:" + power);

		switch (quadrant) {
		case 4:
			velX = opposite;
			velY = adjacent;
			if (velY > 0)
				velY = -velY;
			if (velX < 0)
				velX = -velX;
			break;
		case 3:
			velX = adjacent;
			velY = opposite;
			if (velY > 0)
				velY = -velY;
			if (velX > 0)
				velX = -velX;
			break;
		case 2:
			velX = opposite;
			velY = adjacent;
			if (velY < 0)
				velY = -velY;
			if (velX > 0)
				velX = -velX;
			break;
		case 1:
			velX = adjacent;
			velY = opposite;
			if (velY < 0)
				velY = -velY;
			if (velX < 0)
				velX = -velX;
			break;
		}

		// Because our Y goes up as you go down.
		velY = -velY;

		// log("Angle: " + angle, "theta:" + theta + " X:" + velX + " Y:" +
		// velY);

		return new Vector2(velX, velY);
	}
	
}
